package com.example.ruoyivoicecall.util;

import java.io.*;
import java.nio.file.*;
import java.util.UUID;

/**
 * 文件工具类
 */
public class FileCommon {
    /**
     * 读取文件内容为字符串
     */
    public static String readFileToString(String filePath) throws IOException {
        return Files.readString(Paths.get(filePath));
    }

    /**
     * 将字符串写入文件
     */
    public static void writeStringToFile(String filePath, String content) throws IOException {
        Files.writeString(Paths.get(filePath), content);
    }

    /**
     * 复制文件
     */
    public static void copyFile(String sourcePath, String destinationPath) throws IOException {
        Files.copy(Paths.get(sourcePath), Paths.get(destinationPath), StandardCopyOption.REPLACE_EXISTING);
    }

    /**
     * 删除文件
     */
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        return file.exists() && file.delete();
    }

    /**
     * 生成唯一文件名
     */
    public static String generateUniqueFileName(String originalFileName) {
        String extension = PathUtil.getFileExtension(originalFileName);
        String uniqueId = UUID.randomUUID().toString();
        if (extension.isEmpty()) {
            return uniqueId;
        }
        return uniqueId + "." + extension;
    }

    /**
     * 获取文件大小（字节）
     */
    public static long getFileSize(String filePath) throws IOException {
        return Files.size(Paths.get(filePath));
    }

    /**
     * 检查文件是否存在
     */
    public static boolean isFileExists(String filePath) {
        return Files.exists(Paths.get(filePath));
    }

    /**
     * 创建临时文件
     */
    public static File createTempFile(String prefix, String suffix) throws IOException {
        return File.createTempFile(prefix, suffix);
    }

    /**
     * 读取文件到字节数组
     */
    public static byte[] readFileToByteArray(String filePath) throws IOException {
        return Files.readAllBytes(Paths.get(filePath));
    }

    /**
     * 将字节数组写入文件
     */
    public static void writeByteArrayToFile(String filePath, byte[] data) throws IOException {
        Files.write(Paths.get(filePath), data);
    }
}